"""
这个文件用来读取train_acc.txt，然后画acc和loss曲线
"""
import matplotlib.pyplot as plt
import re
import json
with open("../config.json") as f:
    config = json.load(f)
txt_path = config['train_inform']  #train_acc.txt文件路径

txt_file = open(txt_path,'r')
tra_epochs,tra_loss,tra_lr,tra_acc,val_loss,val_acc = [],[],[],[],[],[]
for line in txt_file.readlines():
    if line[0] == 'E':
        numbers = list(map(float,re.findall(r'\d+\.?\d*',line)))

        epoch = int(numbers[0])
        train_lr = numbers[1]
        train_loss = numbers[2]
        train_acc = numbers[3]
        valid_loss = numbers[4]
        valid_acc = numbers[-1]

        tra_epochs.append(epoch)
        tra_loss.append(train_loss)
        tra_lr.append(train_lr)
        tra_acc.append(train_acc)
        val_loss.append(valid_loss)
        val_acc.append(valid_acc)
txt_file.close()

#------------------------画图--------------------------------------
fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(15, 20), dpi=100)
axs[0][0].plot(tra_epochs,tra_lr,c='green')
axs[0][0].set_xlabel('epoch')
axs[0][0].set_ylabel('lr')
axs[0][0].set_title('lr')

axs[0][1].plot(tra_epochs,tra_loss,c='blue',label='loss')
axs[0][1].set_xlabel('epoch')
axs[0][1].set_ylabel('train_loss')
axs[0][1].set_title('train_loss')

axs[0][2].plot(tra_epochs,tra_acc,c='red',label='acc')
axs[0][2].set_xlabel('epoch')
axs[0][2].set_ylabel('train_acc')
axs[0][2].set_title('train_acc')

axs[1][1].plot(tra_epochs,val_loss,c='red',label='acc')
axs[1][1].set_xlabel('epoch')
axs[1][1].set_ylabel('val_loss')
axs[1][1].set_title('val_loss')

axs[1][2].plot(tra_epochs,val_acc,c='red',label='acc')
axs[1][2].set_xlabel('epoch')
axs[1][2].set_ylabel('val_acc')
axs[1][2].set_title('val_acc')
plt.show()
